package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView

import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import com.gyf.immersionbar.ImmersionBar
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.haoshuang.zhouzhoubang.R


import com.luck.picture.lib.photoview.PhotoView
import java.util.*
import kotlin.collections.ArrayList


/**
 * Copyright (c)
 *
 * @Description
 * @Author ""
 * @Copyright Copyright (c)
 * @Date 2017
 * @CreateBy android_studio
 */
class LookBigImgActivity : AppCompatActivity(){
    private   var vpView: ViewPager? = null
  private  var btnLeft: ImageView? = null
    private  var currentPosition = 0
    var imgPathsBeanList = arrayListOf<String>()
    var mCaches = LinkedList<PhotoView>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_look_big_img)
        ImmersionBar.with(this)
            .statusBarDarkFont(false) //状态栏字体是深色，不写默认为亮色
            .flymeOSStatusBarFontColor(R.color.black) //修改flyme OS状态栏字体颜色
            .init()
        btnLeft = findViewById<View>(R.id.iv_finish) as ImageView
        btnLeft!!.setOnClickListener { finish() }
        vpView = findViewById<View>(R.id.vp_view) as ViewPager
        val arr=intent.getSerializableExtra("imgBeen") as ArrayList<String>
        imgPathsBeanList.clear()
        if (arr!=null){
            imgPathsBeanList.addAll(arr)
        }

        currentPosition = intent.getIntExtra("position", 0)



        vpView!!.pageMargin = (resources.displayMetrics.density * 15).toInt()
      //  vpView!!.addOnPageChangeListener(this)
        vpView!!.adapter = object : PagerAdapter() {
            override fun getCount(): Int {
                return imgPathsBeanList.size
            }

            override fun isViewFromObject(view: View, `object`: Any): Boolean {
                return view === `object`
            }

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
              val  view = if (mCaches.size == 0) {
                    PhotoView(this@LookBigImgActivity)
                } else {
                    mCaches.removeFirst()
                }
                // view.enable();
                view!!.scaleType = ImageView.ScaleType.FIT_CENTER
                val url = imgPathsBeanList[position]
                Glide.with(this@LookBigImgActivity)
                    .load(url)
                    .into(view)

                container.addView(view)
                view.setOnClickListener { finish() }
                return view
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                container.removeView(`object` as View)
            }
        }
        vpView!!.currentItem = currentPosition
    }

  /*  override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
    override fun onPageSelected(position: Int) {}
    override fun onPageScrollStateChanged(state: Int) {}*/
}